package io;

import java.io.File;
import java.io.IOException;
import java.net.URI;

public class FileTest {
    public static void main(String[] args) {
//        createNewFile();
//        mkdir();
//        mkdirs();
//        delete();
//        new Thread(()->deleteOnExit()).start();
//        deleteOnExit();
//        createTempFile();
//        getFileInfo();
//        deleteDir(new File("/Users/luchuan/IdeaProjects/java-test/b"));
//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    /**
     * 创建新文件
     */
    static void createNewFile(){
        // 创建成功的前提是该文件在所目录必须存在，否则抛出异常 java.io.IOException: No such file or directory
        File file = new File("/Users/luchuan/IdeaProjects/java-test/a.txt");
        if (!file.exists()) {
            try {
                boolean flag = file.createNewFile();
                System.out.println(flag);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建临时文件
     */
    static void createTempFile(){
        // 创建成功的前提是该文件在所目录必须存在，否则抛出异常 java.io.IOException: No such file or directory
        File directory = new File("/Users/luchuan/IdeaProjects/java-test");
        if (directory.exists() && directory.isDirectory()) {
            try {
                File tempFile = File.createTempFile("tmp_", ".txt", directory);
                System.out.println(tempFile.getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建一个新的文件夹
     */
    static void mkdir(){
        // 创建成功的前提是该文件在所目录必须存在，否则创建不成功，不会抛异常
        File file = new File("/Users/luchuan/IdeaProjects/java-test/a");
        if (!file.exists()) {
            boolean flag = file.mkdir();
            System.out.println(flag);
        }
    }

    /**
     * 创建多级目录
     */
    static void mkdirs() {
        File file = new File("/Users/luchuan/IdeaProjects/java-test/b/a");
        if (!file.exists()) {
            boolean flag = file.mkdirs();
            System.out.println(flag);
        }
    }

    /**
     * 获取文件的信息
     */
    static void getFileInfo(){
        File file = new File("./a.txt");
        String absolutePath = file.getAbsolutePath();
        System.out.println("absolutePath = " + absolutePath);
        String path = file.getPath();
        System.out.println("path = " + path);
        try {
            String canonicalPath = file.getCanonicalPath();
            System.out.println("canonicalPath = " + canonicalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String name = file.getName();
        System.out.println("name = " + name);
        long freeSpace = file.getFreeSpace();
        System.out.println("freeSpace = " + freeSpace);
        long totalSpace = file.getTotalSpace();
        System.out.println("totalSpace = " + totalSpace);
        long usableSpace = file.getUsableSpace();
        System.out.println("usableSpace = " + usableSpace);
        long length = file.length();
        System.out.println("length = " + length);
        boolean canExecute = file.canExecute();
        System.out.println("canExecute = " + canExecute);
        String parent = file.getParent();
        System.out.println("parent = " + parent);
        boolean isAbsolute = file.isAbsolute();
        System.out.println("isAbsolute = " + isAbsolute);
        long lastModified = file.lastModified();
        System.out.println("lastModified = " + lastModified);
        String[] list = file.list();
//        Arrays.stream(list).forEach(System.out::println);
        URI uri = file.toURI();
        System.out.println("uri.getPath() = " + uri.getPath());
        System.out.println("file.toPath().toString() = " + file.toPath().toString());

    }

    /**
     * 删除单个文件或空文件夹（立刻删除）
     */
    static void delete(){
        // 如果删除的是文件夹，则成功的前提是该文件夹为空
        File file = new File("/Users/luchuan/IdeaProjects/java-test/a");
        if (file.exists()) {
            boolean flag = file.delete();
            System.out.println(flag);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除单个文件或空文件夹，当JVM运行结束的时候才会真正删除
     */
    static void deleteOnExit(){
        // 如果删除的是文件夹，则成功的前提是该文件夹为空
        File file = new File("/Users/luchuan/IdeaProjects/java-test/a");
        if (file.exists()) {
            file.deleteOnExit();
        }
    }

    /**
     * 删除多级目录
     * @param file
     */
    static void deleteDir(File file){
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File f : files) {
                    deleteDir(f);
                }
            } else {
                file.delete(); // 删除文件
            }
            file.delete();//删除空文件夹
        } else {
            System.out.println("该文件不存在");
        }
    }

}
